package cn.wolfcode.p2p.website.web.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.p2p.base.anno.RequiredLogin;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.DateUtil;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.LoginInfoContext;

/**
 * 视频认证控制器
 */
@Controller
public class VideoAuthController {
    @Autowired
    private IVideoAuthService videoAuthService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ILoginInfoService loginInfoService;

    @RequestMapping("/videoAuth")
    @RequiredLogin
    public String toVideoAuth(Model model){
        // 根据当前登录用户, 判断是否已经存在视频认证通过

        UserInfo userInfo = userInfoService.get(LoginInfoContext.getCurrentLoginInfo().getId());
        if (!userInfo.getHasVideoAuth()){
            // 没通过
            // 判断是否提交了实名认证
            if (userInfo.getVideoAuthId() == null){
                // 获取视频客服人员列表
                model.addAttribute("customServices", loginInfoService.listCustomServices());
                // 日期的列表
                model.addAttribute("appointmentDates", DateUtil.getLastDates(new Date(), 5));
                return "videoAuth_apply";
            }else {
                model.addAttribute("auditing", true);
                model.addAttribute("videoAuth", videoAuthService.get(userInfo.getVideoAuthId()));
                return "videoAuth_result";
            }

        }else {
            // 通过
            model.addAttribute("auditing", false);
            model.addAttribute("videoAuth", videoAuthService.get(userInfo.getVideoAuthId()));
            return "videoAuth_result";
        }
    }

    @RequestMapping("/videoAudit_apply")
    @ResponseBody
    public JSONResult applyVideAudit( String appointmentDate, String appointmentTime, Long customServiceId){
        // 接收日期, 时间段, 客服id
        videoAuthService.applyVideAudit(appointmentDate, appointmentTime, customServiceId);

        return new JSONResult();
    }
}
